<?php
	/**
	* Shopping Cart Preview before submit
	* 
	* @author trungdt
	* @since Oct 19, 2011
	* 
	* Change logs
	* 
	*/

	if (!defined('EXPONENT')) exit('');
	
	// if not logged, must redirect back to shopping cart view
	if (!exponent_users_isLoggedIn())
	{
		$params=array();
		$params['modules']="productsmodule";
		$params['action']="cart_view";
		$link=exponent_core_makeLink($params);
		header("Location: {$link}");
		die(); // if 
	}
	
	if (!defined("ABProductsModule_Carts"))
		define("ABProductsModule_Carts","ABShopCart_SessionKey");

	$carts=exponent_sessions_get(ABProductsModule_Carts);
	if ($carts == null)
		$carts = array();
	
	$currency=null; // hhee, trick to save last config, for currency format to cart total
	$cart_count=0;
	$cart_total=0;
	$cart_total_saving=0;
	foreach ($carts as $v)
	{
		$cart_count+=$v->quantity;
		// get config
		$config=$db->selectObject("productsmodule_config", "location_data = '{$v->location_data}'");
		$currency=productsmodule::_GetCurrencyDefault($config->currency_id);
		// calculate total of this cart
		$temp=$v->quantity;
		if ($v->on_sale)
			$temp*=$v->price_sale_bk;
		else
			$temp*=$v->price_bk;
		$cart_total_saving+=$v->price_save_bk * $v->quantity;
		$cart_total+=$temp;
		$v->price_total_value=$temp;
		$temp=productsmodule::_GetCurrencyFormat($currency, $temp);
		$v->price_total=$temp;
	}
	$cart_tax=0.1*$cart_total;
	$cart_total2 = $cart_tax + $cart_total;
	$cart_tax=productsmodule::_GetCurrencyFormat($currency, $cart_tax);
	$cart_total = productsmodule::_GetCurrencyFormat($currency, $cart_total);
	// total included tax
	$cart_total2 = productsmodule::_GetCurrencyFormat($currency, $cart_total2);
	$cart_total_saving_bk = $cart_total_saving;
	$cart_total_saving = productsmodule::_GetCurrencyFormat($currency, $cart_total_saving);
	$cart_shipping = productsmodule::_GetCurrencyFormat($currency, 0);
	
	// for shipping 
	// we should determine what method will we use
	$shipping_registered=null;
	$shipping_manual=null;
	$shipping_info = productsmodule::__Get_User_Shipping($shipping_registered, $shipping_manual);
	// update
	if (isset($_REQUEST['cart_shipping']) && $_REQUEST['cart_shipping'] == 1)
	{
		$shipping_info->info2_name = _ab_safe_html_string($_REQUEST['cart_destination_name']);
		$shipping_info->info2_address = _ab_safe_html_string($_REQUEST['cart_destination_address']);
		$shipping_info->info2_province = _ab_safe_html_string($_REQUEST['cart_destination_province']);
		$shipping_info->info2_city = _ab_safe_html_string($_REQUEST['cart_destination_city']);
		$shipping_info->info2_phone = _ab_safe_html_string($_REQUEST['cart_destination_phone']);
		$shipping_info->info2_note = _ab_safe_html_string($_REQUEST['cart_destination_note']);
	}
	if (isset($shipping_info->user_id)) {
			$db->updateObject($shipping_info,'productsmodule_customerinfo');
	} else {
			$shipping_info->user_id=$user->id;
			$db->insertObject($shipping_info,'productsmodule_customerinfo');
	}
	
	$template=new template("productsmodule","_cart_preview");
	$template->assign("carts", $carts); 
	$template->assign("cart_total", $cart_total); 
	$template->assign("cart_total2", $cart_total2); 
	$template->assign("cart_total_saving_bk", $cart_total_saving_bk); 
	$template->assign("cart_total_saving", $cart_total_saving); 
	$template->assign("cart_tax", $cart_tax); 
	$template->assign("cart_shipping", $cart_shipping); 
	// shipping
	$template->assign("shipping_info", $shipping_info); 
	// shipping method
	$ship_method=0;
	if (isset($_REQUEST['cart_shipping']))
		$ship_method=intval($_REQUEST['cart_shipping']);
	$template->assign("ship_method", $ship_method); 
	// this is the way we check that user has been confirm before checkout or not
	exponent_sessions_set("AB_ProductsModule_ShoppingCart_ShipMethod", $ship_method);
	
	_ab_theme_action_register_title(exponent_lang_getText("cart_confirm"));
	
	$html=$template->render();
	
	$template=new template("productsmodule","_cart_preview_main");
	$template->assign("html", $html); 
	$template->output();
	
	// set session :) for cache this HTML, for later send email 
	// we dont want to show the checkout button (final checkout button)
	// so, remove it
	$html=str_replace("<div class=\"proceedToCheckout\">","<div class=\"proceedToCheckout\" style='display: none;'>", $html);
	exponent_sessions_set("AB_ProductsModule_ShoppingCart_DetailHTML", $html);
?>
